package com.beimi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beimi.controller.utils.Tool;
import com.beimi.domain.Book;
import com.beimi.service.IBookService;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private IBookService iBookService;

    @GetMapping
    public Tool getAll(){
        return new Tool(true,iBookService.list());
    }
    @PostMapping
    public Tool save(@RequestBody Book book)  {
        return new Tool(iBookService.save(book));
    }
    @PutMapping
    public Tool update(@RequestBody Book book){
        return new Tool(iBookService.updateById(book));
    }
    @DeleteMapping("{id}")
    public Tool delete(@PathVariable Integer id){
        return new Tool(iBookService.removeById(id));
    }
    @GetMapping("{id}")
    public Tool getById(@PathVariable Integer id){

        return new Tool(true,iBookService.getById(id));
    }
    @GetMapping("{currentPage}/{pageSize}")
    private Tool getPage(@PathVariable int currentPage,@PathVariable int pageSize){

        return new Tool(true,iBookService.getPage(currentPage,pageSize));
    }
}
